package com.duobaoyu.dby.supply.biz.strategy.export;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.duobaoyu.dby.channel.adapter.enums.biz.refund.AfterSaleStatusEnum;
import com.duobaoyu.dby.channel.adapter.enums.biz.refund.AfterSaleTypeEnum;
import com.duobaoyu.dby.supply.biz.converter.AfterSaleServiceConverter;
import com.duobaoyu.dby.supply.biz.service.refund.AfterSaleService;
import com.duobaoyu.dby.supply.common.biz.bo.refund.AfterSaleContactInfoBO;
import com.duobaoyu.dby.supply.common.biz.dto.refund.AfterSaleApplyQueryDTO;
import com.duobaoyu.dby.supply.common.biz.excel.AfterSaleExportData;
import com.duobaoyu.dby.supply.common.biz.vo.refund.AfterSaleExportVO;
import com.duobaoyu.dby.supply.core.task.enums.BizTaskTypeEnum;
import com.duobaoyu.dby.supply.core.task.strategy.AbstractExportAsyncStrategy;
import com.google.common.collect.Lists;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 订单列表导出策略
 *
 * @Author like
 * @Date 2023-04-26 14:42
 **/
@Service
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
public class AfterSaleApplyExportStrategy extends AbstractExportAsyncStrategy<AfterSaleExportData> {

	private final AfterSaleService afterSaleService;
	private final AfterSaleServiceConverter afterSaleServiceConverter;

	@Override
	protected String bizTaskType() {
		return BizTaskTypeEnum.SUPPLY_CHAIN_PURCHASE_AFTER_SALE_ORDER.getValue();
	}

	@Override
	protected List<AfterSaleExportData> dataSourcePage(String param, Integer pageSize, Integer pageNo) {
		AfterSaleApplyQueryDTO afterSaleApplyQuery = JSON.parseObject(param, AfterSaleApplyQueryDTO.class);

		Page<AfterSaleExportVO> afterSaleExportPage = afterSaleService.getAfterSaleExportPage(Page.of(pageNo,pageSize), afterSaleApplyQuery);
		return toOrderExportData(afterSaleExportPage.getRecords());
	}

	@Override
	protected long dataTotalCount(String param) {
		AfterSaleApplyQueryDTO afterSaleApplyQuery = JSON.parseObject(param, AfterSaleApplyQueryDTO.class);
		return afterSaleService.getAfterSaleExportCount(afterSaleApplyQuery);
	}

	private List<AfterSaleExportData> toOrderExportData(List<AfterSaleExportVO> afterSaleExports) {
		List<AfterSaleExportData> afterSaleExportDataList = Lists.newArrayList();
		if (CollectionUtils.isEmpty(afterSaleExports)) {
			return Lists.newArrayList();
		}
		for (AfterSaleExportVO afterSaleExport : afterSaleExports) {
			AfterSaleContactInfoBO afterSaleContactInfo = JSON.parseObject(afterSaleExport.getContactInfo(), AfterSaleContactInfoBO.class);
			AfterSaleExportData afterSaleExportData = afterSaleServiceConverter.assembleAfterSaleExportData(afterSaleExport);
			if (Objects.nonNull(afterSaleContactInfo)) {
				afterSaleExportData.setCustomerName(afterSaleContactInfo.getCustomerContactName());
				afterSaleExportData.setCustomerMobilePhone(afterSaleContactInfo.getCustomerMobilePhone());
			}

			AfterSaleStatusEnum afterSaleStatusEnum = AfterSaleStatusEnum.getAfterSaleStatusEnum(Integer.valueOf(afterSaleExport.getAfterSaleStatus()));
			if(Objects.nonNull(afterSaleStatusEnum)) {
				afterSaleExportData.setAfterSaleStatus(afterSaleStatusEnum.getDescribe());
			}
			AfterSaleTypeEnum afterSaleTypeEnum = AfterSaleTypeEnum.getAfterSaleTypeEnum(Integer.valueOf(afterSaleExport.getAfterSaleType()));
			if(Objects.nonNull(afterSaleTypeEnum)) {
				afterSaleExportData.setAfterSaleType(afterSaleTypeEnum.getDescribe());
			}
			afterSaleExportDataList.add(afterSaleExportData);
		}
		return afterSaleExportDataList;
	}
}
